<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="query.type" clearable placeholder="请选择收支类型">
        <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="search">{{
        $t('table.search') }}</el-button>
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
    </div>
    <el-table :data="tableData" highlight-current-row border :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }">
      <el-table-column prop="subjects" label="项目名称" sortable />
      <el-table-column prop="type" label="收入/支出" sortable>
        <template slot-scope="{row,$index}">
          {{ row.type == 1 ? '收入' : '支出' }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center">
        <template slot-scope="{row,$index}">
          <el-button :disabled="row.built == 1" type="primary" size="mini" @click="handleUpdate(row)">{{ $t('table.edit') }}</el-button>
          <el-button :disabled="row.built == 1" size="mini" type="danger" @click="handleDelete(row)">{{
        $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="query.pageIndex" :limit.sync="query.pageSize"
      @pagination="getInfo" />
    <el-dialog @closed="hanldeClose" :close-on-click-modal="false" :visible.sync="dialogVisible"
      :title="turngroupRela.id ? '编辑' : '新增'">
      <el-form :model="turngroupRela" :rules="rules" ref="ruleForm" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="subjects">
              <el-input v-model="turngroupRela.subjects" placeholder="请输入项目名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收入/支出" prop="type">
              <el-select style="width: 100%;" v-model="turngroupRela.type" placeholder="请选择支出类型">
                <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogVisible = false">{{ $t('permission.cancel') }}</el-button>
        <el-button type="primary" @click="confirm">{{ $t('permission.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { subjectsPageList, subjectsAdd, subjectsModify, subjectsDel } from '@/api/turngroupRela'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { deepClone } from '../../../utils'


export default {
  name: 'TurngroupRela',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      stateList: [
        {
          label: '收入',
          value: 1
        }, {
          label: '支出',
          value: 2
        }
      ],
      dataArray: [],
      loading: true,
      dialogVisible: false,
      total: 0,
      turngroupRela: {},
      query: {
        pageIndex: 1,
        pageSize: 20,
      },
      tableData: [],
      rules: {
        subjects: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择收入/支出类型', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    hanldeClose() {
      this.$refs['ruleForm'].resetFields();
    },
    async getInfo() {
      const { items, total } = await subjectsPageList(this.query)
      this.tableData = items
      this.total = total
    },
    search() {
      this.query.pageIndex = 1
      this.getInfo()
    },
    handleAdd() {
      this.turngroupRela = Object.assign({})
      this.dialogVisible = true
    },
    handleUpdate(row) {
      this.dialogVisible = true
      this.turngroupRela = deepClone(row)
    },
    handleDelete(row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await subjectsDel(row.id)
        this.$message.success('删除成功!')
        this.getInfo()
      })
    },
    async confirm() {
      this.$refs['ruleForm'].validate(async valid => {
        if (this.turngroupRela.id) {
          await subjectsModify(this.turngroupRela)
          this.$message.success('修改成功!')
          this.getInfo()
          this.dialogVisible = false
        } else {
          await subjectsAdd(this.turngroupRela)
          this.$message.success('添加成功!')
          this.getInfo()
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>
